import { createCheckFlowTemplate, updateCheckFlowTemplate } from '@/api/basic'
import { ref } from 'vue'

/**
 * 自定义审批流模板表单服务
 */
export default function () {
  const checkFlowTemplateFormSaveLoading = ref(false)
  const checkFlowTemplateForm = ref<BaseInfoAPIRequest.CheckFlowTemplateForm>({
    code: '',
    description: '',
    gmtCreate: '',
    gmtUpdate: '',
    id: '',
    name: '',
  })

  const resetCheckFlowTemplateForm = () => {
    checkFlowTemplateForm.value = {
      code: '',
      description: '',
      gmtCreate: '',
      gmtUpdate: '',
      id: '',
      name: '',
    }
  }

  const checkFlowTemplateFormSave = async (templateId?: string) => {
    try {
      checkFlowTemplateFormSaveLoading.value = true
      let res: APIResponse

      if (!templateId) {
        res = (await createCheckFlowTemplate(checkFlowTemplateForm.value)).data
      } else {
        res = (await updateCheckFlowTemplate(checkFlowTemplateForm.value)).data
      }

      checkFlowTemplateFormSaveLoading.value = false
      return res
    } catch (error) {
      checkFlowTemplateFormSaveLoading.value = false
    }
  }

  return {
    checkFlowTemplateForm,
    checkFlowTemplateFormSaveLoading,
    checkFlowTemplateFormSave,
    resetCheckFlowTemplateForm,
  }
}
